/*
 * Copyright (C) 2004, Velocityme Partnership
 * IssueActions.java
 *
 * Created on 12 September 2004, 01:19
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.entity.Issue;
import com.velocityme.session.IssueLocal;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.www.actionforms.nodeactionforms.IssueActionForm;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class IssueActions extends ActivityActions {

    /** Creates a new instance of IssueActions */
    public IssueActions() {
    }

    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Issue issue = lookupIssueBean().findIssue(key, nodeId);
        session.setAttribute("issue", issue);
        session.setAttribute("issueHistory", lookupNodeBean().getHistory(key, issue));
        return mapping.findForward("ACTION_DISPLAY");
    }

    @Override
    public ActionForward showCreate(ActionMapping mapping,
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        configureCreate(session, key);
        IssueActionForm actionAF = new IssueActionForm();
        actionAF.setOwnerId(key.getPerson().getId());
        actionAF.setInterested(true);
        session.setAttribute("issueAF", actionAF);
        Issue issue = new Issue();
        issue.setParentNode(lookupNodeBean().findNode(key, nodeId));
        session.setAttribute("issue", issue);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward showEdit(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        Long nodeId = (Long)request.getAttribute("nodeId");
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Issue issue = lookupIssueBean().findIssue(key, nodeId);
        session.setAttribute("issue", issue);
        configureEdit(session, key, issue);
        IssueActionForm issueAF = new IssueActionForm();
        issueAF.setOwnerId(issue.getOwner().getId());
        issueAF.setTaskTypeId(issue.getTaskStateMachine().getId());
        issueAF.setPercentageComplete(issue.getPercentageComplete());
        issueAF.setDurationTotalMins(issue.getExpectedDuration_min());
        Calendar date = Calendar.getInstance();
        date.setTime(issue.getStartDate());
        session.setAttribute("issueAF", issueAF);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        Long nodeId;
        if (request.getParameter("btnSubmit").equals("OK")) {
            Issue issue = (Issue)session.getAttribute("issue");
            ((IssueActionForm)form).extractIssue(key, issue);
            if (issue.getId() != null) {
                lookupIssueBean().editIssue(key, issue);
            }
            else {
                issue = lookupIssueBean().createIssue(key, issue, ((IssueActionForm)form).isInterested());
            }
            nodeId = issue.getId();
        }
        // Cancel simply displays the original node again
        else {
            nodeId = (Long)session.getAttribute("nodeId");
        }
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }

    private IssueLocal lookupIssueBean() {
        try {
            Context c = new InitialContext();
            return (IssueLocal) c.lookup("java:comp/env/IssueBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
